package com.css.fxfzxczh.modules.report.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.report.entity.ReportTemplate;
import com.css.fxfzxczh.modules.report.repository.ReportTemplateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ReportTemplateRepositoryImpl implements ReportTemplateRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(ReportTemplate reportTemplate) {
        String sql = "insert into xczh_report_template(id,number,name,file_id,create_user,create_time,create_user_id,del_flag,unit) " +
                "values(:id, :number, :name, :fileId, :createUser, :createTime, :createUserId, :delFlag, :unit)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(reportTemplate);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<ReportTemplate> getList() {
        String sql = "SELECT * FROM xczh_report_template where del_flag = '" + YNEnum.N.toString() + "'";
        List<ReportTemplate> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ReportTemplate.class));
        return list;

    }

    @Override
    public ReportTemplate getById(String rtId) {
        String sql = "SELECT * FROM xczh_report_template where id = '" + rtId + "'";
        ReportTemplate reportTemplate = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ReportTemplate.class));
        return reportTemplate;
    }

    @Override
    public void updateTemplate(ReportTemplate template) {
        String sql = "update xczh_report_template set file_id = '" + template.getFileId() + "', update_user_id= '" + template.getUpdateUserId() + "' ,update_time = '" + template.getUpdateTime() + "' where id ='" + template.getId() + "'";
        jdbcTemplate.update(sql);
    }
}
